package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.RoleDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.RoleMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import com.itheima.health.vo.MenuVO;
import com.itheima.health.vo.RoleVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Reference
    private PermissionService permissionService;
    @Reference
    private MenuService menuService;
    @Override
    public Result findPage(QueryPageBean queryPageBean) {

        Page<Role> page=null;
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (queryPageBean.getQueryString() == null) {
               page = page(new Page<Role>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }else {
            wrapper.like("name",queryPageBean.getQueryString());page = page(new Page<Role>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        Result result = new Result(pageResult);
        return result;
    }

    @Override
    public Boolean add(RoleDTO roleDTO) {
        Integer id = roleDTO.getId();
        if (StringUtils.isNotBlank(String.valueOf(id))) {
            //根据id删除中间表数据
            baseMapper.deletePermissionInfoByRoleId(id);
            baseMapper.deleteMenuInfoByRoleId(id);
        }
        Boolean flag = this.saveOrUpdate(roleDTO);
        Integer gid = roleDTO.getId();
        //录入permission中间表
        Integer[] permissionIds = roleDTO.getPermissionIds();
        if (permissionIds != null && permissionIds.length > 0) {
            //中间表循环录入
            for (Integer permissionId : permissionIds) {
                baseMapper.addRoleAndPermissionId(gid,permissionId);
            }
        }
        //录入permission中间表
        Integer[] menuIds = roleDTO.getMenuIds();
        if (menuIds != null && menuIds.length > 0) {
            //中间表循环录入
            for (Integer menuId : menuIds) {
                baseMapper.addRoleAndMenuId(gid,menuId);
            }
        }
        return flag;
    }



    @Override
    public Boolean deleteById(int id) {
        Role role = new Role();
        role.setId(id);
        role.setIs_delete(1);
        boolean flag = updateById(role);
        return flag;
    }

    @Override
    public RoleVO findPermissionIdsbyRoleId(int id) {
        List<MenuVO> listM = baseMapper.findMenuAll();
        int[] menuIds= baseMapper.findMenuIdsbyRoleId(id);

        List<Permission> listP = permissionService.list();
        int[] permissionIds= baseMapper.findPermissionIdsbyRoleId(id);
        RoleVO roleVO = new RoleVO();
        roleVO.setPermissionIds(permissionIds);
        roleVO.setPermissionList(listP);

        roleVO.setMenuVOList(listM);
        roleVO.setMenuIds(menuIds);
        return roleVO;
    }
}
